<template>
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="height: 40px; width: 100%"
  >
    <el-col :span="20">
      <span>{{ nodeData.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <!-- 两个内容 -->
        <el-col>{{ nodeData.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <!-- 下拉菜单 element -->
          <el-dropdown @command="delDepartment">
            <span>操作<i class="el-icon-arrow-down" /> </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                command="add"
                :visible="dialogVisible"
                @close="handleCloseDialog"
              >
                添加子部门
              </el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="edit">
                编辑部门
              </el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="del">
                删除部门
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { reqDelDepartments } from '@/api/departments'
export default {
  props: {
    // 定义传入的属性
    nodeData: {
      type: Object,
      required: true
    },
    // 是否根节点
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dialogVisible: true
    }
  },
  methods: {
    delDepartment(command) {
      if (command === 'add') {
        // console.log('添加')
        this.$emit('opendilog', this.nodeData)
      }
      if (command === 'del') {
        // console.log('删除操作')
        this.$confirm('你确认要进行删除么?', '温馨提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'waring'
        })
          .then(async () => {
            // 删除操作成功
            await reqDelDepartments(this.nodeData.id)
            // 添加提示
            this.$message.success('删除成功')
            // 通知父组件更新
            this.$emit('del-depts')
          })
          .catch((error) => {
            console.log(error)
          })
      }
      if (command === 'edit') {
        // console.log('更新')
        this.$emit('editDepts', this.nodeData)
      }
    },
    handleCloseDialog() {
      // 子传父
      this.$emit('close-dialog', false)
    }
  }
}
</script>
<style lang="sass" scoped></style>
